<?php
class DateUtil {

  public static function getDatetime($time = null, $format = 'Y-m-d H:i:s') {
    if ($time) {
      $dtstr = date($format, $time);
    } else {
      $dtstr = date($format, time());
    }

    return $dtstr;
  }

  public static function formatDatetime($datetime = null, $format = 'Y-m-d H:i:s') {
    if ($datetime === null) {
      return '';
    }

    $dTime = DateUtil::makeTimestamp($datetime);
    return DateUtil::getDatetime($dTime, $format);
  }

  public static function buildDatetime($year, $month, $day, $hour = 0, $minute = 0, $second = 0, $format = 'Y-m-d H:i:s') {
    $dTime = mktime($hour, $minute, $second, $month, $day, $year);
    return date($format, $dTime);
  }

  public static function getDatetimeDiff($date1, $date2) {
    $s = strtotime($date2) - strtotime($date1);

    $d = intval($s / 86400);
    $s -= $d * 86400;
    $h = intval($s / 3600);
    $s -= $h * 3600;
    $m = intval($s / 60);
    $s -= $m * 60;

    return array('d' => $d, 'h' => $h, 'm' => $m, 's' => $s);
  }

  public static function makeTimestamp($string='') {
    if (empty($string)) {
      $time = time();
    } elseif (preg_match('/^\d{14}$/', $string)) {
      $time = mktime(substr($string, 8, 2), substr($string, 10, 2), substr($string, 12, 2),
              substr($string, 4, 2), substr($string, 6, 2), substr($string, 0, 4));
    } elseif (is_numeric($string)) {
      $time = (int)$string;
    } else {
      $time = strtotime($string);
      if ($time == -1 || $time === false) {
        $time = time();
      }
    }
    return $time;
  }

  public static function getTimeElapsed($referencedate = 0, $timepointer = '', $measureby = '', $autotext = true) {
    if ($timepointer == '') {
      $timepointer = time();
    }
    $raw = $timepointer - $referencedate;
    $clean = abs($raw);
    $calcNum = array(array('s', 60), array('m', 60*60), array('h', 60*60*60), array('d', 60*60*60*24), array('y', 60*60*60*24*365));
    $calc = array('s' => array(1, 'second'), 'm' => array(60, 'minute'), 'h' => array(60*60, 'hour'), 'd' => array(60*60*24, 'day'), 'y' => array(60*60*24*365, 'year'));

    if ($measureby == '') {
      $usemeasure = 's';

      for ($i=0; $i<count($calcNum); $i++) {
        if ($clean <= $calcNum[$i][1]) {
          $usemeasure = $calcNum[$i][0];
          $i = count($calcNum);
        }
      }
    } else {
      $usemeasure = $measureby;
    }

    $datedifference = floor($clean/$calc[$usemeasure][0]);

    $prospect = '';

    if ($autotext == true && ($timepointer == time())) {
      if ($raw < 0) {
        $prospect = 'from now';
      } else {
        $prospect = 'ago';
      }
    }

    if ($referencedate != 0) {
      if ($datedifference == 1) {
        return $datedifference . ' ' . $calc[$usemeasure][1] . ' ' . $prospect;
      } else {
        return $datedifference . ' ' . $calc[$usemeasure][1] . 's ' . $prospect;
      }
    } else {
      return 'No input time referenced.';
    }
  }

  /**
   * This function will be used to converts default time stamp in the view to
   * java time stamp for db comparizon
   * phptime * 1000 = java time stamp
   * Current view format is 07/17/2012 03:30 AM
   */
  public static function getJavaTimestampFromViewDate($date) {
    return strtotime($date) * 1000;
  }

  public static function minutesToHours($minutes) {
    $hours = (int)($minutes / 60);
    $minutes -= $hours * 60;
    return sprintf("%d:%02.0f", $hours, $minutes);
  }
}